package com.teacold.customdemo.chapter_one;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

public class PathDemo extends View {
    private Paint mPaint;
    private Path mPath;

    public PathDemo(Context context) {
        super(context);
        init();
    }


    public PathDemo(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public PathDemo(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }


    private void init() {
        mPath = new Path();
        mPaint = new Paint();
//        mPaint.setStyle(Paint.Style.STROKE);
//        mPaint.setColor(Color.RED);
//        mPaint.setStrokeWidth(5);


    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawResets(canvas);
    }

    private void drawResets(Canvas canvas) {
        mPath.setFillType(Path.FillType.INVERSE_WINDING);
//        mPath.reset();//函数类似于新建一个路径对象，它的所有的数据空间都会被回收重新分配，但不会清楚FillType
        mPath.rewind();//函数不会清除内存，但会清楚FillType，而reset()函数则会清除内存，但不会清除Filltype
        mPath.addCircle(100, 100, 50, Path.Direction.CW);
        canvas.drawPath(mPath, mPaint);
    }


    /**
     * 路径画图  填充模式
     * WINDING 当两个图形相交时，取相交部分显示
     * EVEN_ODD 取path所在并不相交的区域
     * INVERSE_WINDING 所在并不相交的区域
     * INVERSE_EVEN_ODD 取path的外部和相交区域
     * 实验结论得：Path.Direction.CW Path画图形的时候两个图形的方向一致则 FillType正确  方向不同（CW&CWW）方向相反
     */
    private void drawPathType(Canvas canvas) {
        mPaint.setColor(Color.RED);
        mPaint.setStyle(Paint.Style.FILL);
        mPath.addRect(100, 100, 300, 300, Path.Direction.CW);
        mPath.addCircle(300, 300, 100, Path.Direction.CCW);
        mPath.setFillType(Path.FillType.WINDING);
        canvas.drawPath(mPath, mPaint);
    }


    /**
     * 根据路径画弧
     */
    private void drawArcPath(Canvas canvas) {//根据路径画弧
        RectF rectF = new RectF(10, 10, 100, 50);
        mPath.addArc(rectF, 10, 100);
        canvas.drawPath(mPath, mPaint);
    }

    /**
     * 画椭圆
     */
    private void drawOval(Canvas canvas) {
        RectF rectF = new RectF(10, 10, 200, 100);
        mPath.addOval(rectF, Path.Direction.CCW);
        canvas.drawPath(mPath, mPaint);
    }

    /**
     * 路径画圆
     */
    private void addCircle(Canvas canvas) {
        mPath.addCircle(100, 100, 50, Path.Direction.CCW);
        canvas.drawPath(mPath, mPaint);
    }

    /**
     * 利用路径不同画 圆角矩形
     */
    private void drawRoundRect(Canvas canvas) {
        RectF rectF = new RectF(50, 50, 240, 200);
        mPath.addRoundRect(rectF, 10, 15, Path.Direction.CCW);//方法一：统一的圆角

        RectF rectF1 = new RectF(290, 50, 480, 200);
        float radii[] = {10, 15, 20, 25, 30, 35, 40, 45};
        mPath.addRoundRect(rectF1, radii, Path.Direction.CW);//方法二:各个角圆角角度不同
        canvas.drawPath(mPath, mPaint);
    }

    /**
     * 关于Path的方向问题
     * 主要用于问题走向
     */
    private void drawpathText(Canvas canvas) {
//        RectF rectF=new RectF(50,50,240,200);
//        mPath.addRect(rectF,Path.Direction.CCW);
//        canvas.drawPath(mPath,mPaint);
//        String text1="苦心人天不负，有志者事竟成";
//        mPaint.setColor(Color.YELLOW);
//        mPaint.setTextSize(35);
//        canvas.drawTextOnPath(text1,mPath,0,18,mPaint);

        RectF rectF1 = new RectF(290, 50, 480, 200);
        mPath.addRect(rectF1, Path.Direction.CW);

        canvas.drawPath(mPath, mPaint);
        String text = "苦心人天不负，有志者事竟成";
        mPaint.setColor(Color.YELLOW);
        mPaint.setTextSize(35);
        canvas.drawTextOnPath(text, mPath, 0, 18, mPaint);


    }


    private void addRect(Canvas canvas) {
        mPaint.setColor(Color.RED);
        RectF rectF = new RectF(50, 50, 240, 200);
        mPath.addRect(rectF, Path.Direction.CCW);
        canvas.drawPath(mPath, mPaint);
        //先画出这两天路径
        mPaint.setColor(Color.BLUE);
        RectF rectF2 = new RectF(290, 50, 480, 200);
        mPath.addRect(rectF2, Path.Direction.CW);
        canvas.drawPath(mPath, mPaint);

    }


    private void addXXX(Canvas canvas) {
        mPath.moveTo(10, 10);
        mPath.lineTo(100, 50);
        RectF rectF = new RectF(100, 100, 150, 150);
        mPath.addArc(rectF, 0, 90);
        canvas.drawPath(mPath, mPaint);

    }

    private void drawArc(Canvas canvas) {//画弧线
        mPath.moveTo(10, 10);
        RectF rectF = new RectF(100, 10, 200, 100);
        mPath.arcTo(rectF, 0, 90);
        canvas.drawPath(mPath, mPaint);
    }

    private void drawTriangle(Canvas canvas) {//路径画三角形
        mPath.moveTo(20, 20);
        mPath.lineTo(20, 60);
        mPath.lineTo(60, 60);
        mPath.close();
        canvas.drawPath(mPath, mPaint);
    }
}

